package com.zdsoft.blp.zongbuju.wode.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/1.
 */
public class ChongZhiMiMaActivity extends BaseActivity implements NetCallback{

    private FanHuiView back;
    private Button bt_wancheng;
    private EditText et_firstpw,et_secondpw;
    private static final Integer PASSWORDSAVE =1;
    @Override
    protected void init() {
        setContentView(R.layout.chongzhimima_wode);
        bt_wancheng = find(R.id.bt_wancheng);
        et_firstpw = find(R.id.et_firstpw);
        et_secondpw = find(R.id.et_secondpw);
        back = find(R.id.back);
        back.getTv_title().setText("重置密码");
        Intent intent = getIntent();
        final String value = intent.getStringExtra("value");
        bt_wancheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String  firstpw = et_firstpw.getText().toString().trim();
                String  secondpw = et_secondpw.getText().toString().trim();

                if(firstpw.equals(secondpw)&&!"".equals(firstpw)){
                    Net net = new Net();
                    net.url = API.MEMBER_URL;
                    net.nameSpace = API.MEMBER_NAMESPACE;
                    net.methodName = API.PASSWORDSAVE;
                    Map<String, Object> map = new HashMap<>();
                    map.put("userId", value);
                    map.put("password", firstpw);
                    net.parms = map;
                    net.code = PASSWORDSAVE;
                    getServer(net, ChongZhiMiMaActivity.this);
                }else {
                    Toast.makeText(getApplicationContext(), "两次密码不一致", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {

        if(flag&&code==PASSWORDSAVE){
            try {
                JSONObject object = new JSONObject(res);
                Toast.makeText(getApplicationContext(), object.getString("msg"), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getApplicationContext(),DengLuActivity.class);
                startActivity(intent);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    }
}
